package com.openbravo.pos.quickfinder;

import java.awt.event.ActionEvent;

import javax.swing.JPanel;
import javax.swing.JToolBar;

import com.openbravo.bean.Product;
import com.openbravo.data.basic.BasicException;
import com.openbravo.data.gui.MessageInf;
import com.openbravo.data.loader.query.QBFParameter;
import com.openbravo.data.loader.query.QBFParameters;
import com.openbravo.data.model.Field;
import com.openbravo.pos.panels.model.ProductTableModel;

import cn.ekuma.epos.datalogic.I_DataLogicERP;
import cn.ekuma.epos.db.table.I_Product;
import cn.ekuma.epos.panel.AbstractJFinderPanel;
import cn.ekuma.util.SwingUtil;

public class JProductFinder extends AbstractJFinderPanel<Product>{
	QBFParameters productSeachQbfParameters;
	QBFParameter code,ref,name,origin,mnemonic;
	
	
	public JProductFinder(I_DataLogicERP dlSales) {
		super(new ProductTableModel(),dlSales );
		code=new QBFParameter(I_Product.CODE);
		ref=new QBFParameter(I_Product.REFERENCE);
		name=new QBFParameter(I_Product.NAME);
		origin=new QBFParameter(I_Product.ORIGIN);
		mnemonic=new QBFParameter(I_Product.MNEMONIC);
		
		productSeachQbfParameters=new QBFParameters();
		productSeachQbfParameters.or(code).or(ref).or(name).or(origin).or(mnemonic);
	}

	@Override
	protected void m_jBarcode1ActionPerformed(ActionEvent e) {
		String sCode=inputCode.getText();
    	if(sCode.isEmpty()){
            return ;
         }
         try {
             if(sCode.matches("^[~～][A-Z0-9]+")){
                   java.util.List<Product> prods=((I_DataLogicERP)dlSales).listProductByMnemonic(sCode.substring(1));
                   if(prods!=null&&prods.size()>0)
                	   tableModel.resert(prods);
             }else{
            	code.equal(sCode);
         		ref.equal(sCode);
         		name.link(sCode);
         		origin.link(sCode);
         		mnemonic.link(sCode);
         		tableModel.resert(dlSales.query(Product.class, productSeachQbfParameters));
             }
             SwingUtil.PreferredJXTableColumn(jXTable1);
         } catch (BasicException eData) {  
             new MessageInf(eData).show(this);           
         }
	}

	@Override
	protected void configNavigator(JPanel jNavigatorToolBar, JPanel jNavigator) {
	         
		
	}

	@Override
	protected void configToolBar(JToolBar jToolBar12) {

	}

	@Override
	protected Field[] getQBFFields() {
		return ProductTableModel.getQBFFields();
	}

	@Override
    protected void query(QBFParameters qbfParameters) {
	  try{
		 tableModel.resert(dlSales.query(Product.class, qbfParameters));
		 SwingUtil.PreferredJXTableColumn(jXTable1);
    } catch (BasicException eData) {  
        new MessageInf(eData).show(this);           
    }
	}
}
